using System;
using Cysharp.Threading.Tasks;
using UnityEngine.UI;

namespace Framework.Helper
{
    public static class UIHelper
    {
        // button点击事件是否完成
        private static bool buttonClickCompeleted = true;

        public static void AddListener(this Button button, Func<UniTask> handler)
        {
            if (handler == null) return;

            button.onClick.RemoveAllListeners();
            
            async UniTask ClickActionAsync() 
            {
                buttonClickCompeleted = false;
                await handler();
                buttonClickCompeleted = true;
            }

            void ClickAction()
            {
                if (!buttonClickCompeleted) return;
                ClickActionAsync().ToCoroutine();
            }

            button.onClick.AddListener(ClickAction);
        }
    }
}